package com.examples;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: zslajchrt
 * Date: 3.5.11
 * Time: 12:59
 */
public class PhotoFilter implements Filter {

    private String name;

    private Date created;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    private String getWhere(List<Object> parameters) {
        List<String> condList = new ArrayList<String>();

        if (name != null) {
            condList.add("p.name=?");
            parameters.add(name);
        }
        if (created != null) {
            condList.add("date(p.created)=?");
            parameters.add(created);
        }

        StringBuilder sb = new StringBuilder();
        for (String cond : condList) {
            if (!"".equals(sb.toString())) {
                sb.append(" AND ");
            }
            sb.append(cond);
        }

        return sb.toString();
    }

    public String getSQL(List<Object> parameterValues) {
        //return "SELECT * FROM Photo p WHERE " + getWhere(parameters);
        return "select p.id as photoId, p.created as created, p.description as description, p.name as photoName, a.name as albumName from Photo p left join Album a on a.name=p.album_name where " + getWhere(parameterValues);
    }

}
